<template>
  <div class="app-container">
    <h1 ref="myh1">App 根组件</h1>
    <button @click="changeColor">改变组件颜色</button>
    <button @click="resetLeft">重置Left组件中的count值</button>

    <hr />

    <input type="text" v-if="inputVisible" @blur="showButton" ref="iptref" />
    <button @click="showInput" v-else>展示输入框</button>

    <hr />

    <div class="box">
      <!-- 渲染 Left 组件和 Right 组件 -->
      <Left ref="Left1"></Left>
    </div>
  </div>
</template>

<script>
import Left from "@/components/Left.vue";

export default {
  data() {
    return {
      // 控制输入框和按钮的按需切换；
      // 默认值为 false，表示默认展示按钮，隐藏输入框
      inputVisible: false,
    };
  },
  /* updated() {
    this.$refs.iptRef.focus()
  }, */
  methods: {
    changeColor() {
      console.log(this);
      this.$refs.myh1.style.color = "skyblue";
    },
    resetLeft() {
      this.$refs.Left1.resetCount();
    },
    showInput() {
      // 展示文本框
      this.inputVisible = true;
      // console.log(this.$refs.iptref);  undefined 因为刚展示出来的文本框还未被来得及渲染到页面上就被获取

      // this.$nextTick(cb)将回调延时到下一个DOM更新周期之后去执行，从而保证回调能够获取到最新的DOM元素
      this.$nextTick(() => {
        this.$refs.iptref.focus();
      });
    },
    showButton() {
      // 展示按钮
      this.inputVisible = false;
    },
  },
  components: {
    Left,
  },
};
</script>

<style lang="less">
.app-container {
  padding: 1px 20px 20px;
  background-color: #efefef;
}
.box {
  display: flex;
}
</style>
